package main

// 这个文件基本上就是测试 server下的http和Websocket服务是否好使

import (
	"fmt"
	"golang.org/x/net/websocket"
	http2 "im/server/http"
	myWs "im/server/websocket"
	"net/http"
)

func main() {
	runWs()
	runHttp()

}

// http
func runHttp() {
	ser := http2.NewServer()

	ser.AddRoute("/index", index)
	//http.HandleFunc("/index", index2)
	http.Handle("/", ser) // todo 注意：如果自定义http请求的方法（ListenAndServe第二个参数不是nil）,如果想同时用一个端口去监听http和Websocket要加这行代码
	http.ListenAndServe(":8000", nil)
}

func index(ctx *http2.Context) {

}

func index2(w http.ResponseWriter, r *http.Request) {

}

// Websocket
func runWs() {
	myWs.OnHandSheKe = func(config *websocket.Config, request *http.Request) error {
		fmt.Println("自定义握手协议")
		//return fmt.Errorf("null origin")
		return nil
	}

	myWs.OnOpen = func(server *myWs.Server, conn *websocket.Conn) {
		fmt.Println("建立连接")
	}

	myWs.OnMessage = func(server *myWs.Server, frame *myWs.Frame) {
		fmt.Println("接收的消息 ：", frame.Data)
		server.Send("你好！客服端")
	}

	myWs.OnClose = func(server *myWs.Server, conn *websocket.Conn) {
		fmt.Println("关闭连接")
	}

	myWs.NewServer(":3333", "/ws")
}
